﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace BeeCoders.Tools.ControlCanvas.Adorners
{
    public class ResizeThumb : Thumb
    {
        private DesignerItem designerItem;
        private DesignerCanvas designerCanvas;

        public ResizeThumb()
        {
            DragStarted += ResizeThumbDragStarted;
            DragDelta += ResizeThumbDragDelta;

            MouseDown += ResizeThumbMouseDown;
        }

        static void ResizeThumbMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Console.WriteLine("ok");
        }

        private void ResizeThumbDragStarted(object sender, DragStartedEventArgs e)
        {
            designerItem = DataContext as DesignerItem;

            if (designerItem != null)
            {
                designerCanvas = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;
            }
        }

        private void ResizeThumbDragDelta(object sender, DragDeltaEventArgs e)
        {
            if (designerItem != null && designerCanvas != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop = double.MaxValue;
                double minDeltaHorizontal = double.MaxValue;
                double minDeltaVertical = double.MaxValue;

                double snapHorInterval = designerCanvas.SnapOffsetX;
                double snapVerInterval = designerCanvas.SnapOffsetY;

                foreach (DesignerItem item in designerCanvas.SelectedItems)
                {
                    minLeft = Math.Min(Canvas.GetLeft(item), minLeft);
                    minTop = Math.Min(Canvas.GetTop(item), minTop);

                    minDeltaVertical = Math.Min(minDeltaVertical, item.ActualHeight - item.MinHeight);
                    minDeltaHorizontal = Math.Min(minDeltaHorizontal, item.ActualWidth - item.MinWidth);
                }

                foreach (DesignerItem item in designerCanvas.SelectedItems)
                {
                    double dragDeltaVertical;
                    switch (VerticalAlignment)
                    {
                        case VerticalAlignment.Bottom:
                            dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);

                            if (designerCanvas.IsSnapToGrid)
                            {
                                dragDeltaVertical = snapVerInterval * Math.Round(dragDeltaVertical / snapVerInterval);

                            }

                            item.Height = item.ActualHeight - dragDeltaVertical;
                            item.Control.Height = (int)item.Height;
                            break;
                        case VerticalAlignment.Top:
                            dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);

                            if (designerCanvas.IsSnapToGrid)
                            {
                                dragDeltaVertical = snapVerInterval * Math.Round(dragDeltaVertical / snapVerInterval);

                            }
                            Canvas.SetTop(item, Canvas.GetTop(item) + dragDeltaVertical);
                            item.Height = item.ActualHeight - dragDeltaVertical;
                            item.Control.Height = (int)item.Height;

                            break;
                    }

                    double dragDeltaHorizontal;
                    switch (HorizontalAlignment)
                    {
                        case HorizontalAlignment.Left:
                            dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);

                            if (designerCanvas.IsSnapToGrid)
                            {
                                dragDeltaHorizontal = snapHorInterval * Math.Round(dragDeltaHorizontal / snapHorInterval);

                            }

                            Canvas.SetLeft(item, Canvas.GetLeft(item) + dragDeltaHorizontal);
                            item.Width = item.ActualWidth - dragDeltaHorizontal;
                            item.Control.Width = (int)item.Width;

                            break;
                        case HorizontalAlignment.Right:
                            dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);

                            if (designerCanvas.IsSnapToGrid)
                            {
                                dragDeltaHorizontal = snapHorInterval * Math.Round(dragDeltaHorizontal / snapHorInterval);

                            }

                            item.Width = item.ActualWidth - dragDeltaHorizontal;
                            item.Control.Height = (int)item.Height;
                            item.Control.Width = (int)item.Width;

                            break;
                    }
                }

                e.Handled = true;
            }
        }
    }
}
